package com.zigong.app.ui.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.ruihang.generalibrary.utils.ListUtils;
import com.zigong.app.R;
import com.zigong.app.beans.NewsItem;
import com.zigong.app.beans.ServiceDataBean;
import com.zigong.app.ui.adapter.holder.VideoPlayHolder;
import com.zigong.app.ui.utils.GlideUtil;
import com.zigong.app.ui.widget.BasePlayerView;

import java.util.List;

public class ServiceRecentlyUsedServiceAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<ServiceDataBean> dataList;
    private Context ctx;
    private OnServiceItemClickListener serviceItemClickListener;


    static class RecentlyServiceViewHolder extends RecyclerView.ViewHolder {
        LinearLayout layout;
        CustomFontTextView serviceName;
        ImageView serviceIcon;

        public RecentlyServiceViewHolder(View view) {
            super(view);
            layout = (LinearLayout) view.findViewById(R.id.service_layout);
            serviceName = (CustomFontTextView) view.findViewById(R.id.service_name);
            serviceIcon = (ImageView) view.findViewById(R.id.service_icon);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        ctx = parent.getContext();
        View v = v = LayoutInflater.from(parent.getContext()).inflate(R.layout.service_list_item, parent, false);
        holder = new RecentlyServiceViewHolder(v);
        return holder;
    }

    public void setData(List<ServiceDataBean> list) {
        if (null != list) {
            dataList = list;
        }
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        return 0;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final ServiceDataBean info = dataList.get(position);
        final RecentlyServiceViewHolder viewHolder = (RecentlyServiceViewHolder) holder;
        GlideUtil.getGlid(ctx, info.getCommIcon()).into(viewHolder.serviceIcon);
        viewHolder.serviceName.setText(info.getCommName());
        viewHolder.layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != serviceItemClickListener) {
                    serviceItemClickListener.onServiceItemClick(info);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        if (ListUtils.isListValued(dataList)) {
            return dataList.size();
        } else {
            return 0;
        }
    }

    public void setServiceItemClickListener(OnServiceItemClickListener serviceItemClickListener) {
        this.serviceItemClickListener = serviceItemClickListener;
    }

    public interface OnServiceItemClickListener {
        void onServiceItemClick(ServiceDataBean item);
    }
}
